import request from '../utils/request'

// 登录
export const login = (username, password) => {
  const formData = new URLSearchParams()
  formData.append('username', username)
  formData.append('password', password)
  
  return request.post('/api/connect/token', formData, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  })
}

// 注册
export const register = (username, password) => {
  return request.post('/api/connect/register', {
    username,
    password,
    email: ''
  })
}

// 获取用户信息
export const getUserInfo = () => {
  return request.get('/api/connect/userinfo')
}

// 退出登录
export const logout = () => {
  return request.post('/auth/logout')
}

// 获取公开博客列表（首页）
export const getBlogList = (params) => {
  return request.get('/api/blogs', { params })
}

// 获取我的博客列表
export const getUserBlogList = (params) => {
  return request.get('/api/user/blogs', { params })
}

// 获取公开博客详情
export const getBlogDetail = (id) => {
  return request.get(`/api/blogs/${id}`)
}

// 获取我的博客详情
export const getUserBlogDetail = (id) => {
  return request.get(`/api/user/blogs/${id}`)
}

// 创建博客
export const createBlog = (data) => {
  return request.post('/api/user/blogs', data)
}

// 更新博客
export const updateBlog = (data) => {
  return request.put('/api/user/blogs', data)
}

// 删除博客
export const deleteBlog = (id) => {
  return request.delete(`/api/user/blogs/${id}`)
}

// 获取博客分类列表
export const getBlogCategories = () => {
  return request.get('/api/blogs/categories')
}

// 获取博客分类列表（带文章数量）
export const getCategoriesWithNum = () => {
  return request.get('/api/blogs/categories/num')
}

// 获取标签列表
export const getTagList = () => {
  return request.get('/api/blogs/tags')
}

// 获取我的博客统计数据
export const getUserBlogStats = () => {
  return request.get('/api/user/blogs/num')
}